using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Headless;
using Avalonia.Platform;

namespace Klear.UnitTest;

public class AvaloniaTestBase
{
    public static readonly TestApp Application;

    static AvaloniaTestBase()
    {
        // 防止多次初始化
        if (Application != null) return;
            
        // 初始化 Avalonia
        var appBuilder = BuildAvaloniaApp();
        Application = (TestApp)appBuilder.Instance;
            
        // 设置应用程序生命周期
        var lifetime = new ClassicDesktopStyleApplicationLifetime
        {
            Args = Array.Empty<string>()
        };
            
        Application.Initialize();
        Application.RegisterServices();
        Application.OnFrameworkInitializationCompleted();
    }

    private static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<TestApp>()
            .UseHeadless(new AvaloniaHeadlessPlatformOptions
            {
                UseHeadlessDrawing = true
            })    // 使用无头模式
            .UseSkia()        // 使用 Skia 渲染
            .SetupWithoutStarting();
}

public class TestApp : Application
{
    public override void RegisterServices()
    {
        // 在这里注册任何需要的服务
    }
}